package com.yc.thread.ticketDemo;

import java.util.concurrent.ThreadPoolExecutor;

// 模拟线程并发
public class TicketDemoSale {

    // 模拟并发操作
    public static void main(String[] args) {
        Ticket ticket = new Ticket();

        new Thread(() -> {sale(ticket);}, "张三").start();
        new Thread(() -> {sale(ticket);}, "李四").start();
        new Thread(() -> {sale(ticket);}, "王五").start();
    }

    private static void sale(Ticket ticket) {
        for (int i = 0; i < 60; i++) {
            ticket.sale();
        }
    }
}

// 线程资源类
class Ticket {
    // 属性
    private int ticket_number = 50;

    public synchronized void sale() {
        if (ticket_number > 0) {
            System.out.println(Thread.currentThread().getName() + "卖出了" + (ticket_number--) + "票，剩余：" + ticket_number);
        }
    }
}